package micro.beauty.common.tool.convert;

import com.baomidou.mybatisplus.core.metadata.IPage;
import micro.beauty.common.core.page.PageDTO;

import java.util.List;
import java.util.stream.Collectors;

/**
* @Description:    实体类转DTO 基类
* @Author:         liuliya
* @CreateDate:     2020/4/21 17:49
*/
public abstract class BaseEntityConverter<E, D> {

    public abstract D entity2Dto(E entity);

    public List<D> list2Dto(List<E> list) {
        return list.stream().map(this::entity2Dto).collect(Collectors.toList());
    }

    public PageDTO<D> page2Dto(IPage<E> page) {
        List<D> records = list2Dto(page.getRecords());
        PageDTO<D> pageDto = new PageDTO<>(page.getCurrent(), page.getSize(), page.getTotal());
        pageDto.setRecords(records);
        return pageDto;
    }

}
